//
//  PushStrimViewController.m
//  PushStrimTest
//
//  Created by zhanlan_1 on 2018/2/5.
//  Copyright © 2018年 zhanlan_1. All rights reserved.
//

#import "PushStrimViewController.h"
#import <AlivcLivePusher/AlivcLivePusherHeader.h>

@interface PushStrimViewController ()<AlivcLivePusherInfoDelegate,AlivcLivePusherErrorDelegate,AlivcLivePusherNetworkDelegate>

@property (nonatomic, strong)AlivcLivePusher *livePusher;
@property (nonatomic, strong)AlivcLivePushConfig *config;

@property (nonatomic, strong)NSString *pushUrlString;

@end

@implementation PushStrimViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [AlivcLivePusher showDebugView];
    
    self.pushUrlString = @"rtmp://video-center.alivecdn.com/livetest/312ee3cb4c1c45cea55382e6cc54936c?vhost=gdtvvod.weixinmvp.top&auth_key=1517885219-0-0-ab6c67e6527beab27009fa29103c327c";
    
    self.view.backgroundColor = [UIColor colorWithWhite:.90 alpha:1];
    [self configPush];
    [self backBtnInstall];
    
}

- (void)backBtnInstall {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.backgroundColor = [UIColor redColor];
    backBtn.frame = CGRectMake(20, 40, 40, 25);
    [backBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
}

- (void)click:(UIButton *)btn {
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}


- (void)configPush {
    
    _config = [[AlivcLivePushConfig alloc] init];
    _config.resolution = AlivcLivePushResolution360P;
    _config.fps = AlivcLivePushFPS20;
    _config.qualityMode = AlivcLivePushQualityModeFluencyFirst;
    _config.videoEncodeGop = AlivcLivePushVideoEncodeGOP_2;
    _config.connectRetryInterval = 2000;
    _config.previewMirror = false;
    
    _config.beautyOn = false;
    
    _config.orientation = AlivcLivePushOrientationLandscapeRight;
    
    NSString *iconPath = [[NSBundle mainBundle] pathForResource:@"180" ofType:@"png"];
    [_config addWatermarkWithPath:iconPath watermarkCoordX:.8 watermarkCoordY:.7 watermarkWidth:.1];
    
    
    _config.targetVideoBitrate = 1200; //  目标码率1200Kbps
    _config.minVideoBitrate = 400; //  最小码率400Kbps
    _config.initialVideoBitrate = 900;
    _config.beautyMode = AlivcLivePushBeautyModeProfessional;
    // 美白范围0-100
    _config.beautyWhite = 70;
    // 磨皮范围0-100
    _config.beautyBuffing = 40;
    // 红润设置范围0-100
    _config.beautyRuddy = 40;
    /* 下面接口是人脸识别下的高级美颜参数 */
    // 大眼设置范围0-100
    _config.beautyBigEye = 30;
    // 瘦脸设置范围0-100
    _config.beautyThinFace = 40;
    // 收下巴设置范围0-100
    _config.beautyShortenFace = 50;
    // 腮红设置范围0-100
    _config.beautyCheekPink = 15;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.livePusher = [[AlivcLivePusher alloc] initWithConfig:_config];
        [self configPusher];
    });
}

- (void)configPusher {
    
    [self.livePusher setInfoDelegate:self];
    [self.livePusher setErrorDelegate:self];
    [self.livePusher setNetworkDelegate:self];
    
//    [self.livePusher setTargetVideoBitrate:800];
//    [self.livePusher setMinVideoBitrate:200];
    
//    if (success == 0) {
//        NSLog(@"success 配置成功");
//
//        int pushStatus =
//        NSLog(@"推送结果 --- %d",pushStatus);
//    }
    
    [self.livePusher startPreview:self.view];
    
    [self.livePusher switchCamera];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.livePusher startPushWithURL:self.pushUrlString];
    });
}

/**
 推流开始回调
 
 @param pusher 推流AlivcLivePusher
 */
- (void)onPushStarted:(AlivcLivePusher *)pusher {
    
    NSLog(@"--- 开始推流");
    
    
}

- (void)onPushStoped:(AlivcLivePusher *)pusher {
    
    NSLog(@"--- 停止推流");
}

#pragma mark - AlivcLivePusherNetworkDelegate
/**
 网络差回调
 */
- (void)onNetworkPoor:(AlivcLivePusher *)pusher {
    
    NSLog(@"网络差回调");
}


/**
 推流链接失败
 @param error error
 */
- (void)onConnectFail:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error {
    NSLog(@"推流链接失败");
}


/**
 网络恢复
 */
- (void)onConnectRecovery:(AlivcLivePusher *)pusher {
    NSLog(@"网络恢复");
}


/**
 重连开始回调
 */
- (void)onReconnectStart:(AlivcLivePusher *)pusher {
    NSLog(@"重连开始回调");
}


/**
 重连成功回调
 */
- (void)onReconnectSuccess:(AlivcLivePusher *)pusher {
    NSLog(@"重连成功回调");
}


/**
 重连失败回调
 @param error error
 */
- (void)onReconnectError:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error {
    [self.livePusher reconnectPushAsync];
}


/**
 发送数据超时
 */
- (void)onSendDataTimeout:(AlivcLivePusher *)pusher {
    NSLog(@"发送数据超时");
}

#pragma mark - 出现错误回调
/**
 系统错误回调
 @param error error
 */
- (void)onSystemError:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error {
    
    NSLog(@"error = %@",error.errorDescription);
    
}

/**
 SDK错误回调
 @param error error
 */
- (void)onSDKError:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error {
    NSLog(@"error = %@",error.errorDescription);
    
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
//    [self.livePusher restartPush];
    
    
    NSString *pushUrl = [self.livePusher getPushURL];
    
    NSLog(@"--- %d ------ %@",[self.livePusher isPushing],pushUrl);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
